package com.itheima.health.controller;

import com.itheima.health.common.MessageConst;
import com.itheima.health.entity.Result;
import com.itheima.health.service.MemberService;
import com.itheima.health.util.ValidateCodeUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.concurrent.TimeUnit;

/**
 * 验证码Controller
 */
@RestController
@RequestMapping("/mobile/validateCode")
@Slf4j
public class MobileValidateCodeController {
    @Autowired
    private MemberService memberService;
    @Autowired
    private RedisTemplate redisTemplate;

    @PostMapping("/send")
    public Result sendMessage(@RequestParam(value = "telephone") String phoneNumber, String type, HttpSession session) {
        //生成4位数字的验证码;
        String code = ValidateCodeUtils.generateValidateCode(4).toString();
        //模拟手机接收验证码;
        System.out.println("验证码为" + code);
        //在手机号前面拼接当前是登录用的手机号;
        String phone = type + "_" + phoneNumber;
        //输出拼接完的手机号;
        System.out.println(phone);
        //存到redis中;
        redisTemplate.opsForValue().set(phone, code, 10, TimeUnit.MINUTES);
        return new Result(true, MessageConst.SEND_VALIDATECODE_SUCCESS);

    }
}
